﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.Windows.Forms;

namespace TeachMe.Application.Controls
{
    public class LanguagesComboBox : ComboBox
    {
        private bool _modified;

        public void Fill()
        {
            Items.Clear();

            foreach (Languages.Item item in Singleton<Languages>.Instance.Items)
            {
                Items.Add(item);
            }
        }

        public string Code
        {
            get
            {
                return SelectedItem == null ? Text : ((Languages.Item)SelectedItem).Code;
            }
            set
            {
                Languages.Item item = Singleton<Languages>.Instance.GetByCode(value);

                if (item != null)
                {
                    SelectedItem = item;
                }
                else
                {
                    Text = value;
                }
            }
        }

        protected override void OnTextChanged(System.EventArgs e)
        {
            _modified = true;
            
            base.OnTextChanged(e);
        }

        protected override void OnSelectedItemChanged(System.EventArgs e)
        {
            _modified = true;
            
            base.OnSelectedItemChanged(e);
        }

        public bool Modified
        {
            get
            {
                return _modified;
            }
            set
            {
                _modified = value;
            }
        }
    }
}
